require(['modules/fetch'],function(fetch){
     
    var loadStoryListFn = function (){
        fetch('listStory', { ps: 20, pn: 0},
            function (result) {
                if(0 == result.errno ){
                    vueMainData.sidebarDataList = result.data.list;
                }
            },
            function(data) {
                console.log("ajax faild");
            }
        );
    }
    
    //main
    var vueMainData = {
        title:'',
        content:'',
        content_preview:'',
        story_id: 0,
        sidebarDataList: [],
    };
    new Vue({
        el: "#vue-app",
        data: vueMainData,
        methods:{
            submitwriteStoryForm: function(){
                console.log(arguments)
                console.log(vueMainData);

                var formData = {
                    title: vueMainData.title,
                    content: vueMainData.content,
                }
                var model = {};
                if(!isNaN(vueMainData.story_id) && vueMainData.story_id > 0){
                    model = URLS.editChapter;
                    formData.story_id = vueMainData.story_id;
                }
                fetch('postStory', formData,
                    function (result) {
                        if(0 == result.errno){
                            alert("发布成功!");
                            location.href="/story/readStory?story_id=" + result.data.story_id;
                            loadStoryList();
                        } else {
                            alert("发布失败!");
                        }
                    },
                    function(data) {
                        console.log("ajax faild");
                    }
                );
                return false;
            },
            editStoryContentChange: function(){

                vueMainData.content_preview = markdown.toHTML(vueMainData.content);
                console.log(vueMainData.content_preview);
            },
            sidebarItemClick: function(item,index){

                location.href="/story/readStory?story_id=" + parseInt(item.story_id);
                console.log(index,item);
            }
        }
    });
    loadStoryListFn();
});